package com.xiangge.array;

import java.util.Arrays;
import java.util.Scanner;

public class Supermarket {
    static Scanner input = new Scanner(System.in);//构建输入对象
    public static void main(String[] args) {
        //预创建数据
        String[][] vip = getvipData();


        while (true) {
            //主菜单
            System.out.println("=============欢迎使用超市用户管理系统=======");
            System.out.println("1.查询会员信息");
            System.out.println("2.删除会员");
            System.out.println("3.新增会员信息");
            System.out.println("4.退出");
            System.out.println("========================================");
            //输入
            System.out.print("请输入菜单选项：");
            int menu = input.nextInt();


            switch (menu){
                case 1:
                    chaxun(vip);
                    break;
                case 2:
                    System.out.print("请输入删除会员id号：");
                    int delnum = input.nextInt();
                    delete(delnum,vip);
                    break;
                case 3:
                    add(vip);
                    break;
                default:
                    System.out.println("==================已退出==================");
                    return;
            }
        }
    }

    public static String[][] getvipData() {
        String[][] vip = new String[10][];
        vip[0]= new String[]{"1001", "冯程程", "铂金卡", "2000", "合法账户"};
        vip[1]= new String[]{"1003", "孙悟空", "钻石卡", "5000", "被禁账户"};
        vip[2]= new String[]{"1002", "张巧巧", "黄金卡", "500 ", "合法账户"};
        /*for (int i = 0; i < vip.length; i++) {
            System.out.println(Arrays.toString(vip[i]));
        }*/
        return vip;
    }

    public static void add(String[][] vip) {
        System.out.println("请输入新增的用户名：");
        String name = input.next();
        System.out.println("请选择卡类型：(金卡 铂金卡 钻石卡)");
        String card = input.next();
        System.out.println("请输入会员积分：");
        int points = input.nextInt();
        if (points<0){
            System.err.println("积分不能为负数");
            return;
        }
        int newid = (int)(Math.random()*(9999+1-1000)) + 1000;

        for (int i = 0; i < vip.length; i++) {
            if (vip[i]==null){
                continue;
            }
            String num = vip[i][0];
            int id = Integer.valueOf(num).intValue();
            if (newid==id){
                System.err.println("有重复id");
                return;
            }
        }
        String newVid = String.valueOf(newid);
        String newPoints = String.valueOf(points);
        String[] newvip = {newVid,name,card,newPoints,"合法账户"};

        for (int i = 0; i < vip.length; i++) {
            if (vip[i]==null){
                vip[i]=newvip;
                break;
            }
        }
        chaxun(vip);
        return;
    }

    public static void delete(int delnum, String[][] vip) {
        for (int i = 0; i < vip.length; i++) {
            if (vip[i]==null){
                break;
            }
            String num = vip[i][0];
            int num1 = Integer.valueOf(num).intValue();
            if (num1==delnum) {
                System.out.println("您要删除的数据为："+
                        Arrays.toString(vip[i]));
                System.out.println("您确定要删除吗（y/n）");
                String yn = input.next();
                if (yn.equals("y")){
                    vip[i]=null;
                }
                chaxun(vip);
                return;
            }
        }
        System.out.println("查无此人");


    }

    public static void chaxun(String[][] vip) {
        System.out.println("用户ID\t账号\t\t卡类型\t积分\t\t卡片状态");
        for (int i = 0; i < vip.length; i++) {
            if (vip[i]==null){
                continue;
            }
            System.out.print(vip[i][0]+"\t"+vip[i][1]+"\t"+vip[i][2]+"\t"+vip[i][3]+"\t"+vip[i][4]+"\n");


        }
        return;

    }
}
